/**
 * 自定义模板测试
 * */

var docArys = [];


//去上传
function toUpload(){
    window.location.href = 'item_upload.html';
}


//去文档
function toDoc(){
    window.location.href = 'item_doc.html';
}


//返回
function back(){
    window.location.href = 'item_tmp.html';
}


//项目模板列表
function getTmpList(){
    $.ajax({
        type: 'post',
        url: domain + '/tmp/index',
        data: {  },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                alert(res.msg);
                return
            }
            var d = res.data;
            var h = '', i;
            for (i = 0; i < d.length; i ++) {
                h += '<div class="item-li" style="float: left;" title="项目ID为' + d[i].id + '" onclick="apply(' + d[i].id + ');">';
                h += d[i].title;
                h += '</div>';
            }
            $('#itemList').html(h);
        },
        error: function() {
            console.log('错误啦');
        }
    });
}


//领取模板
function apply(v){
    $.ajax({
        type: 'post',
        url: domain + '/tmp/user/apply',
        data: { token: '2b47d0965162d6e4071a2ccea919255f', item_id: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                alert(res.msg);
                return
            }
            alert('领取成功');
            getUserItemList();
        },
        error: function() {
            console.log('错误啦');
        }
    });
}


//用户项目列表
function getUserItemList(){
    $.ajax({
        type: 'post',
        url: domain + '/tmp/user/index',
        data: {  },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                alert(res.msg);
                return
            }
            var d = res.data;
            var h = '', i;
            for (i = 0; i < d.length; i ++) {
                h += '<div class="item-li" style="float: left;" title="用户项目ID为' + d[i].id + '" onclick="getItemOne(' + d[i].id + ');">';
                h += d[i].title;
                h += '</div>';
            }
            $('#itemUserList').html(h);
        },
        error: function() {
            console.log('错误啦');
        }
    });
}


//项目详情
function getItemOne(v){
    itemid = v;
    $.ajax({
        type: 'post',
        url: domain + '/tmp/user/one',
        data: { item_id: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                alert(res.msg);
                return
            }
            var view1 = res.data.treeList.front_view;
            var api1 = res.data.treeList.front_api;
            var view2 = res.data.treeList.admin_view;
            var api2 = res.data.treeList.admin_api;
            var h = '', i;
            //前台页面
            h += '<div class="item-li" style="background: #555; color: #eee;">前台页面</div>';
            for (i = 0; i < view1.length; i ++) {
                h += '<div class="item-li" onclick="getDetail(' + view1[i].fid + ');" title="' + view1[i].fpath + '">';
                h += '>>';
                h += view1[i].fname;
                h += '</div>';
            }
            h += '<div style="height: 5px;"></div>';
            //前台接口
            h += '<div class="item-li" style="background: #555; color: #eee;">前台接口</div>';
            for (i = 0; i < api1.length; i ++) {
                h += '<div class="item-li" onclick="getApiOne(' + api1[i].id + ');">';
                h += '>>';
                h += api1[i].title;
                h += '</div>';
            }
            h += '<div style="height: 5px;"></div>';
            //后台页面
            h += '<div class="item-li" style="background: #555; color: #eee;">后台页面</div>';
            for (i = 0; i < view2.length; i ++) {
                h += '<div class="item-li">';
                h += '>>';
                h += view2[i].fname;
                h += '</div>';
            }
            h += '<div style="height: 5px;"></div>';
            //后台接口
            h += '<div class="item-li" style="background: #555; color: #eee;">后台接口</div>';
            for (i = 0; i < api2.length; i ++) {
                h += '<div class="item-li" onclick="getApiOne(' + api2[i].id + ');">';
                h += '>>';
                h += api2[i].title;
                h += '</div>';
            }
            $('#treeList').html(h);
        },
        error: function() {
            console.log('错误啦');
        }
    });
}


//获取文件内容
function getDetail(v){
    fid = v;
    $.ajax({
        type: 'post',
        url: domain + '/tmp/user/file/read',
        data: { fid: fid },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                alert(res.msg);
                return
            }
            $('#detail').html('<textarea id="editor" style="width: 100%; height: 100%; resize: none;">' + res.data + '</textarea>');
        },
        error: function() {
            console.log('错误啦');
        }
    });
}


//保存当前文件
function saveFile(){
    if (fid == 0) {
        alert('没有文件被编辑');
        return
    }
    var formData = {
        fid: fid,
        fdetail: $('#editor').val()
    };
    //console.log(formData);
    //return
    $.ajax({
        type: 'post',
        url: domain + '/tmp/user/file/save',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                alert(res.msg);
                return
            }
            alert('保存成功');
        },
        error: function() {
            console.log('错误啦');
        }
    });
}


//获取接口详情
function getApiOne(v){
    $.ajax({
        type: 'post',
        url: domain + '/xm/doc/one',
        data: { doc_id: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                alert(res.msg);
                return
            }
            var d = res.data;
            var h = '';
            h += '<div style="margin: 20px;">';
            h += '<div>';
            h += '接口名称：' + d.title;
            h += '</div>';
            h += '<div>';
            h += '接口地址：' + d.url;
            h += '<input type="hidden" id="testUrl" value="' + d.url + '"/>';
            h += '</div>';
            h += '<div>';
            h += '请求参数：';
            h += '<textarea style="width: 100%; min-height: 100px;" id="testParam">' + d.req_json + '</textarea>';
            h += '</div>';
            h += '<div>';
            h += '<button onclick="sendTest();">点击请求</button>';
            h += '</div>';
            h += '<div>';
            h += '返回结果：';
            h += '<div style="width: 100%; height: 200px; background: #fff; overflow: auto;" id="backJson"></div>';
            h += '</div>';
            h += '</div>';
            $('#detail').html(h);
        },
        error: function() {
            console.log('错误啦');
        }
    });
}


//测试请求
function sendTest(){
    var testUrl = $('#testUrl').val();
    var testParam = $('#testParam').val();
    var div = $('#backJson');
    div.html('请求地址：\n'+testUrl+'；\n'+'请求参数：\n'+testParam);
    $.ajax({
        type: 'post',
        url: testUrl,
        data: testParam,
        success: function (res) {
            console.log(res);
            div.html('<pre>'+JSON.stringify(res, null, 2));
        },
        error: function() {
            console.log('错误啦');
            div.html('错误啦');
        }
    });
}


//导出当前项目
function exportItem(){
    if (itemid == 0) {
        alert('没有项目要导出');
        return
    }
    var formData = {
        token: '2b47d0965162d6e4071a2ccea919255f',
        item_id: itemid
    };
    var div = $('#down');
    div.html('正在导出...');
    $.ajax({
        type: 'post',
        url: domain + '/tmp/user/export',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                alert(res.msg); return
            }
            //window.location.href = res.data;
            div.html('<a href="'+res.data+'" target="_blank">点击下载</a>');
        },
        error: function() {
            console.log('错误啦');
        }
    });
}


//文档列表
function docList(){
    var div = $('#tbody');
    $.ajax({
        type: 'post',
        url: '/api/admin/tmp/doc/index',
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                div.html('<tr><td colspan="10">没有记录</td></tr>');
            }
            var d = res.data;
            docList = res.data;
            var h = '', i;
            for (i = 0; i < d.length; i ++) {
                h += '<tr>';
                h += '<td>';
                h += d[i].client_type==1 ? '前台' : '后台';
                h += '</td>';
                h += '<td>' + d[i].item_id + '</td>';
                h += '<td>' + d[i].title + '</td>';
                h += '<td>' + d[i].url + '</td>';
                h += '<td>';
                h += '<a href="javascript:;" style="padding: 0; background: none; color: #0088cc;" onclick="docOne(' + d[i].id + ');">详情</a>';
                h += '</td>';
                h += '</tr>';
            }
            $('tbody').html(h);
        },
        error: function () {
            console.log('错误啦');
        }
    });
}


//文档详情
function docOne(v){
    //console.log(docList);
    if (docList.length == 0) {
        alert('没有记录列表'); return
    }
    var docOne = '', i;
    for (i = 0; i < docList.length; i ++) {
        if (docList[i].id == v) {
            docOne = docList[i];
        }
    }
    if (docOne == '') {
        alert('没有记录'); return
    }
    $('#id')[0].value = v;
    $('#item_id')[0].value = docOne.item_id;
    $('#client_type')[0].value = docOne.client_type;
    $('#title')[0].value = docOne.title;
    $('#url')[0].value = docOne.url;
    $('#req_json')[0].value = docOne.req_json;
}


//文档保存
function docModify(){
    var formData = {
        id: $('#id').val(),
        item_id: $('#item_id').val(),
        client_type: $('#client_type').val(),
        title: $('#title').val(),
        url: $('#url').val(),
        req_json: $('#req_json').val(),
    };
    if (!formData.item_id) {
        alert('项目必须的'); return
    }
    var httpUrl = '';
    if (formData.id == 0) {
        httpUrl = '/api/admin/tmp/doc/add';
    } else {
        httpUrl = '/api/admin/tmp/doc/modify';
    }
    //console.log(formData, httpUrl);
    //return
    $.ajax({
        type: 'post',
        url: httpUrl,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                alert(res.msg); return
            }
            top.location.reload();
        },
        error: function () {
            console.log('错误啦');
        }
    });
}


//获取地址栏中参数
function getParamByUrl(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r==null) return '';
    return  unescape(r[2]);
}